package com.example.cloudpracticeplatform.utils;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import org.springframework.stereotype.Component;

import java.net.URISyntaxException;
import java.time.Duration;
import java.util.UUID;

public class DockerUtils {
    // 连接docker
    public static DockerClient connect() throws URISyntaxException {
        String host = "tcp://127.0.0.1:2375";
        String apiVersion = "1.38";
        //创建DefaultDockerClientConfig 
        DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withApiVersion(apiVersion)
                .withDockerHost(host)
                .build();
        //创建DockerHttpClient 
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .maxConnections(100)
                .connectionTimeout(Duration.ofSeconds(30))
                .responseTimeout(Duration.ofSeconds(45))
                .build();
        //创建DockerClient 
        DockerClient client = DockerClientImpl.getInstance(config, httpClient);
        return client;
    }

    // 获取docker信息
    public static void dockerInfo() throws URISyntaxException {
        DockerClient client = connect();
        Info info = client.infoCmd().exec();
        System.out.println("docker info : " + info.toString());
    }

    // 创建容器
//    public static CreateContainerResponse createContainer1(DockerClient client) throws URISyntaxException {
//
//        CreateContainerCmd containerCmd = client.createContainerCmd("nginx:latest")
//                //名字
//                .withName("nginx-01")
//                //端口映射 内部80端口与外部81端口映射
//                .withHostConfig(new HostConfig().withPortBindings(new Ports(new ExposedPort(80), Ports.Binding.bindPort(81))))
//                //环境变量
//                .withEnv("key=value")
//                //挂载
//                .withVolumes(new Volume("/var/log"));
//
//        //创建
//        CreateContainerResponse response = containerCmd.exec();
//        System.out.println(response.getId());
//        return response;
//    }
    // 创建容器
    public static CreateContainerResponse createContainer(String imageId,Integer port,String username,String password) throws URISyntaxException {
        DockerClient client = connect();
        // 定义暴露的端口和端口绑定
        ExposedPort exposedPort = ExposedPort.tcp(4000);
        Ports portBindings = new Ports();
        portBindings.bind(exposedPort, Ports.Binding.bindPort(port));
        // 定义环境变量
        String[] envVars={
                "USER="+username,
                "PASSWORD="+password,
                "REMOTE_DESKTOP=kasmvnc"
        };
        // 定义共享内存大小
        long shmSize = 1024 * 1024 * 1024; // 1024m 转换为字节
        // 创建主机配置
        HostConfig hostConfig = new HostConfig()
                .withPortBindings(portBindings)
                .withShmSize(shmSize);

        // 创建容器命令
        CreateContainerCmd containerCmd = client.createContainerCmd(imageId)
                .withName(UUID.randomUUID().toString().replace("-", "").substring(0,8))
                .withEnv(envVars)
                .withHostConfig(hostConfig);
        // 创建容器
        CreateContainerResponse response = containerCmd.exec();
        System.out.println("Created container with ID: " + response.getId());
        return response;
    }
    
    //启动容器
    public static void startContainer(String containerId) throws URISyntaxException {
        DockerClient client = connect();
        //启动
        client.startContainerCmd(containerId).exec();
    }

    //停止容器
    public static void stopContainer(String containerId) throws URISyntaxException {
        DockerClient client = connect();
        client.stopContainerCmd(containerId).exec();
    }
    
    // 删除容器
    public static void removeContainer(String containerId) throws URISyntaxException {
        DockerClient client = connect();
        client.removeContainerCmd(containerId).exec();
    }




}
